﻿package org.dm.display 
{
	import flash.events.EventDispatcher;
	import org.dm.utils.List;
	import org.dm.utils.Map;
	import org.dm.general.ClassGather;
	/**
	 * ...
	 * @author Scott
	 */
	public class DisplayMgr extends EventDispatcher
	{
		private static var _instance:DisplayMgr;
		
		private var _container:Map;
		private var _list:Map;
		
		public static function get instance():DisplayMgr
		{ 
			if(_instance==null)
			{
				var name:String = "org.dm.motion.DisplayMsg";
				_instance = ClassGather.getClass(name) as org.dm.display.DisplayMgr;
				if(_instance==null)
				{
					_instance = new org.dm.display.DisplayMgr();
					ClassGather.reg(name,_instance);
				}
			}
			return _instance;
		}
		
		public function DisplayMgr() 
		{
			_container = new Map();
			_list = new Map();
		}
		
		public static function add(key:String,data:*):void 
		{
			if (key == null) return;
			var keyName:Array = key.split(".");
			var miniName:String = keyName.pop();
			var group:DisplayMgr = instance.createGroup(keyName);
			group._container.insert(miniName, data);
		}
		
		public static function getDisplay(key:String,classkey:String=null):* 
		{
			if (key == null) return;
			if (classkey == null) classkey = key;
			var keyName:Array = key.split(".");
			var miniName:String = keyName.pop();
			var group:DisplayMgr = instance.createGroup(keyName);
			if (group._container.find(miniName) == null)
			{
				group._container.insert(miniName, ClassGather.getInstance(classkey));
			}
			return group._container.find(miniName).data;
		}
		
		public function remove(key:String):void 
		{
			_container.remove(key);
		}
		
		//public function addGroup(groupName:String):DisplayMgr 
		//{
			//var dm:DisplayMgr = _list.find(groupName);
			//if (dm != null) return dm;
			//_list.insert(groupName, new DisplayMgr());
			//return _list.find(groupName);
		//}
		
		private function findGroup(groupName:Array):DisplayMgr 
		{
			var len:int = groupName.length;
			var depthGroup:Array = new Array();
			depthGroup[0] = instance;
			while (len > 0)
			{
				var result:DisplayMgr = depthGroup[depthGroup.length - 1]._list.find(groupName[groupName.length - len]);
				if (result == null)
				{
					return null;
				}
				depthGroup.push(result);
				len--;
			}
			return depthGroup[depthGroup.length - 1];
		}
		
		public function createGroup(groupName:Array):DisplayMgr 
		{
			var len:int = groupName.length;
			var olen:int = len;
			var depthGroup:Array = new Array();
			depthGroup[0] = instance;
			while (len > 0)
			{
				var result:* = depthGroup[depthGroup.length - 1]._list.find(groupName[olen - len]);
				if (result == null)
				{
					depthGroup[depthGroup.length - 1]._list.insert(groupName[olen - len], new DisplayMgr());
				}
				depthGroup.push(depthGroup[depthGroup.length - 1]._list.find(groupName[olen - len]).data);
				len--;
			}
			return depthGroup[depthGroup.length - 1] as DisplayMgr;
		}
		
		public function removeGroup(groupName:String):void 
		{
			_list.remove(groupName);
		}
	}

}